// KPI Indicator Data Management
// This file contains the default indicator data and data management functions

// Default indicator data structure
const defaultIndicatorData = [
    {
        id: '1',
        parentId: '',
        chineseName: '质量',
        englishName: 'Quality',
        scoreMin: 0,
        scoreMax: 100,
        scoringRules: '根据工作完成质量、效率和创新程度进行综合评分',
        sortOrder: 1,
        remarks: '核心绩效指标',
        createTime: '2024-01-01T09:00:00',
        children: [
            {
                id: '1-1',
                parentId: '1',
                chineseName: '产品报废或返工',
                englishName: 'Scrap or ReworkPPM',
                scoreMin: 0,
                scoreMax: 100,
                scoringRules: '按时完成率：90%以上得90-100分，80-90%得80-89分，以此类推',
                sortOrder: 1,
                remarks: '按时完成工作任务的比例',
                createTime: '2024-01-01T09:15:00'
            },
            {
                id: '1-2',
                parentId: '1',
                chineseName: '客户投诉',
                englishName: 'Customer Complaints',
                scoreMin: 0,
                scoreMax: 100,
                scoringRules: '无错误得100分，轻微错误得85-99分，严重错误得60-84分',
                sortOrder: 2,
                remarks: '工作输出的质量水平',
                createTime: '2024-01-01T09:30:00'
            },
            {
                id: '1-3',
                parentId: '1',
                chineseName: '操作员工作指南',
                englishName: 'Working Instruction',
                scoreMin: 0,
                scoreMax: 100,
                scoringRules: '无错误得100分，轻微错误得85-99分，严重错误得60-84分',
                sortOrder: 2,
                remarks: '工作输出的质量水平',
                createTime: '2024-01-01T09:30:00'
            },
            {
                id: '1-4',
                parentId: '1',
                chineseName: '检验员工作指南',
                englishName: 'Inspection Intruction',
                scoreMin: 0,
                scoreMax: 100,
                scoringRules: '无错误得100分，轻微错误得85-99分，严重错误得60-84分',
                sortOrder: 2,
                remarks: '工作输出的质量水平',
                createTime: '2024-01-01T09:30:00'
            }
        ]
    },
    {
        id: '2',
        parentId: '',
        chineseName: '效率',
        englishName: 'Efficiency',
        scoreMin: 0,
        scoreMax: 100,
        scoringRules: '评估团队合作精神、沟通能力和协调配合度',
        sortOrder: 2,
        remarks: '团队合作相关指标',
        createTime: '2024-01-01T10:00:00',
        children: [
            {
                id: '2-1',
                parentId: '2',
                chineseName: '产出',
                englishName: 'Output',
                scoreMin: 0,
                scoreMax: 100,
                scoringRules: '同事评价：优秀90-100分，良好80-89分，一般70-79分',
                sortOrder: 1,
                remarks: '与同事和上级的沟通效果',
                createTime: '2024-01-01T10:15:00'
            },
            {
                id: '2-2',
                parentId: '2',
                chineseName: '调机/换模/换型时间',
                englishName: 'Set up/Change over',
                scoreMin: 0,
                scoreMax: 100,
                scoringRules: '同事评价：优秀90-100分，良好80-89分，一般70-79分',
                sortOrder: 1,
                remarks: '与同事和上级的沟通效果',
                createTime: '2024-01-01T10:15:00'
            },
            {
                id: '2-3',
                parentId: '2',
                chineseName: '样件',
                englishName: 'Prototype',
                scoreMin: 0,
                scoreMax: 100,
                scoringRules: '同事评价：优秀90-100分，良好80-89分，一般70-79分',
                sortOrder: 1,
                remarks: '与同事和上级的沟通效果',
                createTime: '2024-01-01T10:15:00'
            }
        ]
    },
    {
        id: '3',
        parentId: '',
        chineseName: '安全',
        englishName: 'Safety',
        scoreMin: 0,
        scoreMax: 100,
        scoringRules: '评估个人学习能力、技能提升和职业发展',
        sortOrder: 3,
        remarks: '个人成长相关指标',
        createTime: '2024-01-01T11:00:00',
        children: [
            {
                id: '3-1',
                parentId: '3',
                chineseName: '安全事故',
                englishName: 'Accident Case',
                scoreMin: 0,
                scoreMax: 100,
                scoringRules: '同事评价：优秀90-100分，良好80-89分，一般70-79分',
                sortOrder: 1,
                remarks: '与同事和上级的沟通效果',
                createTime: '2024-01-01T10:15:00'
            },
            {
                id: '3-2',
                parentId: '3',
                chineseName: '安全隐患',
                englishName: 'Accident Risk',
                scoreMin: 0,
                scoreMax: 100,
                scoringRules: '同事评价：优秀90-100分，良好80-89分，一般70-79分',
                sortOrder: 1,
                remarks: '与同事和上级的沟通效果',
                createTime: '2024-01-01T10:15:00'
            },
            {
                id: '3-3',
                parentId: '3',
                chineseName: '安全事故通报及时性',
                englishName: 'In-time Report',
                scoreMin: 0,
                scoreMax: 100,
                scoringRules: '同事评价：优秀90-100分，良好80-89分，一般70-79分',
                sortOrder: 1,
                remarks: '与同事和上级的沟通效果',
                createTime: '2024-01-01T10:15:00'
            }
        ]
    },
    {
        id: '4',
        parentId: '',
        chineseName: '5S',
        englishName: '5S',
        scoreMin: 0,
        scoreMax: 100,
        scoringRules: '评估个人学习能力、技能提升和职业发展',
        sortOrder: 3,
        remarks: '个人成长相关指标',
        createTime: '2024-01-01T11:00:00',
        children: [
            {
                id: '4-1',
                parentId: '4',
                chineseName: '按照5S标准',
                englishName: 'Follow 5S Standard',
                scoreMin: 0,
                scoreMax: 100,
                scoringRules: '同事评价：优秀90-100分，良好80-89分，一般70-79分',
                sortOrder: 1,
                remarks: '与同事和上级的沟通效果',
                createTime: '2024-01-01T10:15:00'
            }
        ]
    },
    {
        id: '5',
        parentId: '',
        chineseName: '出勤',
        englishName: 'Attendance',
        scoreMin: 0,
        scoreMax: 100,
        scoringRules: '评估个人学习能力、技能提升和职业发展',
        sortOrder: 3,
        remarks: '个人成长相关指标',
        createTime: '2024-01-01T11:00:00',
        children: [
            {
                id: '5-1',
                parentId: '5',
                chineseName: '病假',
                englishName: 'Sick Leave',
                scoreMin: 0,
                scoreMax: 100,
                scoringRules: '同事评价：优秀90-100分，良好80-89分，一般70-79分',
                sortOrder: 1,
                remarks: '与同事和上级的沟通效果',
                createTime: '2024-01-01T10:15:00'
            },
            {
                id: '5-2',
                parentId: '5',
                chineseName: '事假',
                englishName: 'Private Leave',
                scoreMin: 0,
                scoreMax: 100,
                scoringRules: '同事评价：优秀90-100分，良好80-89分，一般70-79分',
                sortOrder: 1,
                remarks: '与同事和上级的沟通效果',
                createTime: '2024-01-01T10:15:00'
            },
            {
                id: '5-3',
                parentId: '5',
                chineseName: '异常考勤',
                englishName: 'Abnormal Time Record',
                scoreMin: 0,
                scoreMax: 100,
                scoringRules: '同事评价：优秀90-100分，良好80-89分，一般70-79分',
                sortOrder: 1,
                remarks: '与同事和上级的沟通效果',
                createTime: '2024-01-01T10:15:00'
            },
            {
                id: '5-4',
                parentId: '5',
                chineseName: '服从安排',
                englishName: 'Follow Assignment',
                scoreMin: 0,
                scoreMax: 100,
                scoringRules: '同事评价：优秀90-100分，良好80-89分，一般70-79分',
                sortOrder: 1,
                remarks: '与同事和上级的沟通效果',
                createTime: '2024-01-01T10:15:00'
            }
        ]
    },
    {
        id: '6',
        parentId: '',
        chineseName: '数据准确性',
        englishName: 'Data Accuracy',
        scoreMin: 0,
        scoreMax: 100,
        scoringRules: '评估个人学习能力、技能提升和职业发展',
        sortOrder: 3,
        remarks: '个人成长相关指标',
        createTime: '2024-01-01T11:00:00',
        children: [
            {
                id: '6-1',
                parentId: '6',
                chineseName: '数据真实性',
                englishName: 'Data Authenticity',
                scoreMin: 0,
                scoreMax: 100,
                scoringRules: '同事评价：优秀90-100分，良好80-89分，一般70-79分',
                sortOrder: 1,
                remarks: '与同事和上级的沟通效果',
                createTime: '2024-01-01T10:15:00'
            },
            {
                id: '6-2',
                parentId: '6',
                chineseName: '数据准确性',
                englishName: 'Data Accuracy',
                scoreMin: 0,
                scoreMax: 100,
                scoringRules: '同事评价：优秀90-100分，良好80-89分，一般70-79分',
                sortOrder: 1,
                remarks: '与同事和上级的沟通效果',
                createTime: '2024-01-01T10:15:00'
            },
            {
                id: '6-3',
                parentId: '6',
                chineseName: '数据及时性',
                englishName: 'Data Timeliness',
                scoreMin: 0,
                scoreMax: 100,
                scoringRules: '同事评价：优秀90-100分，良好80-89分，一般70-79分',
                sortOrder: 1,
                remarks: '与同事和上级的沟通效果',
                createTime: '2024-01-01T10:15:00'
            },
            {
                id: '6-4',
                parentId: '6',
                chineseName: '书写清晰、完整',
                englishName: 'Clear & Complete Filling',
                scoreMin: 0,
                scoreMax: 100,
                scoringRules: '同事评价：优秀90-100分，良好80-89分，一般70-79分',
                sortOrder: 1,
                remarks: '与同事和上级的沟通效果',
                createTime: '2024-01-01T10:15:00'
            }
        ]
    }
];

// Data management functions
const IndicatorDataManager = {
    // Load indicator data from localStorage or return default data
    loadIndicatorData() {
        const saved = localStorage.getItem('indicatorList');
        if (saved) {
            return JSON.parse(saved);
        }
        return this.getDefaultData();
    },

    // Get default indicator data
    getDefaultData() {
        return JSON.parse(JSON.stringify(defaultIndicatorData));
    },

    // Save indicator data to localStorage
    saveIndicatorData(indicatorList) {
        const flatList = this.convertTreeToFlat(indicatorList);
        localStorage.setItem('indicatorList', JSON.stringify(flatList));
    },

    // Convert tree structure to flat array for storage
    convertTreeToFlat(indicatorList) {
        const flatList = [];
        const addToFlat = (items) => {
            items.forEach(item => {
                const flatItem = { ...item };
                delete flatItem.children;
                flatList.push(flatItem);
                if (item.children && item.children.length > 0) {
                    addToFlat(item.children);
                }
            });
        };
        addToFlat(indicatorList);
        return flatList;
    },

    // Convert flat array to tree structure
    convertFlatToTree(flatList) {
        const majorCategories = flatList.filter(item => item.level === 'major');
        const subCategories = flatList.filter(item => item.level === 'sub');

        majorCategories.forEach(major => {
            major.children = subCategories
                .filter(sub => sub.parentId === major.id)
                .sort((a, b) => a.sortOrder - b.sortOrder);
        });

        return majorCategories.sort((a, b) => a.sortOrder - b.sortOrder);
    },

    // Reset data to default
    resetToDefault() {
        const defaultData = this.getDefaultData();
        this.saveIndicatorData(defaultData);
        return defaultData;
    },

    // Clear all data
    clearAll() {
        localStorage.removeItem('indicatorList');
    }
};

// Export for use in other files
if (typeof module !== 'undefined' && module.exports) {
    module.exports = { defaultIndicatorData, IndicatorDataManager };
}